package com.projectsexception.festivales.news;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.projectsexception.festivales.news.model.Feed;

public class SimpleFeedReader {
    
    public static Feed getFeed(URL feedURL) {
        InputSource is = null;
        XMLReader xmlreader = null;
        String encoding = detectEncoding(feedURL);
        FeedHandler feedHandler = new FeedHandler();
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            xmlreader = parser.getXMLReader();
            
            is = new InputSource(feedURL.openStream());
            if (encoding != null) {
                is.setEncoding(encoding);
            }
            
            xmlreader.setContentHandler(feedHandler);
            xmlreader.parse(is);
            
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        } catch (ParserConfigurationException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (SAXException e) {
            System.out.println(e.getMessage());
        }
        return feedHandler.getFeed();
    }
    
    private static String getFirstLine(InputStream is) {
        if (is != null) {
            String line = null;
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is), 1024);
                line = reader.readLine();
            } catch (IOException e) {
                // 
            }
            return line;
        } else {        
            return "";
        }
    }
    
    private static String detectEncoding(URL urlObj) {
        String encoding = null;
        try {
            URLConnection connection = urlObj.openConnection();
            encoding = connection.getContentEncoding();
            if (encoding == null || encoding.equalsIgnoreCase("none")) {
                InputStream is = connection.getInputStream();
                String line = getFirstLine(is).toLowerCase();
                Pattern pattern = Pattern.compile("encoding=[\"']([\\w\\-_]+)[\"']");
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    encoding = matcher.group(1);
                }
                is.reset();
            }
        } catch (MalformedURLException e) {
            //
        } catch (IOException e) {
            // 
        }
        return encoding;
    }


}
